<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
set_html('title','登录商户');
if($_RQ['op']=='get'){
	$_SESSION['merchid']='';
	$_SESSION['lmerchid']='';
	$return['set']=$this->plugin_setting('setting');
	$return['set']['loginlogo']=tomedia($return['set']['loginlogo']);
	$return['set']['loginbg']=tomedia($return['set']['loginbg']);
	if($_SESSION['os']=='windows'){
		$return['session_id']=session_id();
	}
	if($_SESSION['uid'] && !$_RQ['loginup']){
		exi('','error','jy_merch/manage.index/');
	}
	$_SESSION['logout_forward']='jy_merch/manage.login/';
	$_SESSION['forward_url']='jy_merch/manage.index/'; 
	$return['forward_url']=base64_encode($_SESSION['forward_url']);
	$return['info']=array();
	$return['openid']=$_SESSION['openid'];
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['password'] || !$post['username']){
		exi('请填写账户名和密码','error');
	}
	$user=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'uname'=>$post['username']));
	if(!$user){
		$worker = pdo_get('jy_merch_worker',array('uniacid'=>$_SESSION['uniacid'],'jobnum'=>$post['username']));
	}

    if($user && !$worker){
        $mroleid = 1;
        if($post['password']!=base64_decode($user['upass'])){
    		exi('用户名或密码错误','error');
    	}
    }
    if($worker && !$user){
        $mroleid = 2;
    	if(!$worker['merchid'] || $post['password']!=base64_decode($worker['pass'])){
    		exi('用户名或密码错误','error');
    	}
    }   
    if(!empty($user['mid'])){       
        if(!$user['mid']){
            //exi('该商户没有绑定用户/管理员！','error');
        }
    }
	if(!empty($worker['mid'])){
        if(!$worker['mid']){
            //exi('该商户没有绑定用户/管理员！','error');
        }
    }
	if($worker){
	    $_SESSION['uid']=$worker['mid'];
	    $_SESSION['merchid']=$_SESSION['lmerchid']=$_SESSION['onemerchid']=$worker['merchid'];
	    $_SESSION['member']['username']=$worker['jobnum'];
	}else{
	    $_SESSION['uid']=$user['mid'];
 	    $_SESSION['merchid']=$_SESSION['lmerchid']=$_SESSION['onemerchid']=$user['id'];
	    $_SESSION['member']['username']=$user['uname'];
	}
	if($_SESSION['uid']){
		$_SESSION['uid']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'id');
	}
	$_SESSION['uid']=$_SESSION['uid']?:cfc('fans')->get_mid();
	if(!$_SESSION['uid']){
		do{
			$username='user_'.random(10);
		}while(pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$username),'id'));
		$member=array('username'=>$username,'password'=>trim($post['password']));
		$uid=model('core/member')->register($member);
		if($worker){
			pdo_update('jy_merch_worker',array('mid'=>$uid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker['id']));
		}else{
			pdo_update('jy_merch_user',array('mid'=>$uid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		}
		$_SESSION['uid']=$uid;
	}
    $_SESSION['mroleid'] = $mroleid;
	unset($_SESSION['workertype']);
	//绝对地址，会刷新页面

	if(!$_SESSION['wxapp_id']){
		if($_SESSION['os']=='windows'){
            $url= custom_url('jy_merch/manage.statistics.index/',true); 
        }else{
            $url= custom_url('jy_merch/manage.index/',true);
        }
	}else{
		$url='jy_merch/manage.index/';
	}
	exi('登陆成功!','',$url);
}
if($_RQ['op']=='onelogin'){
	if(!$_RQ['upass'] || !$_RQ['uname']){
		exi('请填写账户名和密码','error');
	}
	$user=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'uname'=>$_RQ['uname']));
	if(!$user || $_RQ['upass']!=$user['upass']){
		exi('用户名或密码错误','error');
	}
	if(!$user['mid']){
		exi('该商户没有绑定用户/管理员！','error');
	}
	$_SESSION['uid']=$user['mid'];
	$_SESSION['merchid']=$_SESSION['onemerchid']=$user['id'];
	$_SESSION['member']['username']=$user['uname'];
	$_SESSION['mroleid'] = $mroleid;
	//绝对地址，会刷新页面
	if(!$_SESSION['wxapp_id']){

		if($_SESSION['os']=='windows'){
            $url= custom_url('jy_merch/manage.statistics.index/',true);
            
        }else{
            $url= custom_url('jy_merch/manage.index/',true);
        }
	}else{
		$url='jy_merch/manage.index/';
	}
	exi('','',$url);
}